package cc.rengu.igas.channel.wechat.core.service.trans;


import cc.rengu.igas.channel.wechat.common.constant.WechatConfigConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MchntService;
import cc.rengu.igas.channel.wechat.facade.request.BindAppidRequest;
import cc.rengu.igas.channel.wechat.facade.response.BindAppidResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;


/**
 * 服务商子商户新增对应APPID关联API
 */
public class BindAppidService extends MchntService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        BindAppidRequest bindAppidRequest = new BindAppidRequest();
        ConvertUtil.convertOutput(bindAppidRequest);
        return bindAppidRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        BindAppidResponse bindAppidResponse = new BindAppidResponse();
        BindAppidRequest bindAppidRequest = (BindAppidRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        ConvertUtil.convertInput(bindAppidRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, bindAppidRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_MCHNT_NO, dstChannelCfg.getProviderMchntNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_APP_ID, dstChannelCfg.getAppId());
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID,
                WechatConfigConstant.WECHAT_CONF_NODE,WechatConfigConstant.WECHAT_BIND_APPID_URL);
        if(sysParam == null){
            rglog.error("获取设置支付目录url失败，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(),RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE,sysParam.getParamValue().replace("{merId}",dstChannelCfg.getProviderMchntNo()));
        //走 WechatShortClient 直连微信
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(),0);

        if(xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)){
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }

        if (0 != iReturnCode && 98 != iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        } else if (98 == iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(bindAppidResponse);
        String resultCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RESULT_CODE);
        String returnCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_CODE);
        String returnMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_MSG);
        String errCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_CODE);
        String errMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_MSG);
        if (!WechatParamConstant.STRING_SUCCESS.equals(resultCode)) {
            bindAppidResponse.setRespCode((null == errCode) ? returnCode : errCode);
            bindAppidResponse.setRespDesc((null == errMsg) ? returnMsg : errMsg);
            rglog.error("调用第三方服务失败,respCode=[{}],respDesc=[{}]", bindAppidResponse.getRespCode(), bindAppidResponse.getRespDesc());
        } else {
            bindAppidResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bindAppidResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(bindAppidResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        BindAppidResponse bindAppidResponse = (BindAppidResponse) response.getResult();
        ConvertUtil.convertInput(bindAppidResponse);
    }

}
